<?php
namespace app\common\model;
class User extends Base{
    protected $pk = 'user_id';
    
    public function groupGetUser(array &$data, array $field = []){
        $fields = array_merge(['avatar', 'nickname'], $field);
        unset($field);
        
        $userIds = [];
        
        if($data){
            
            foreach ($data as $item) {
                if(!in_array($item['user_id'], $userIds)){
                    array_push($userIds, $item['user_id']);
                }
            }
            
            unset($item);
            
            $userList = $this->where(['user_id' => $userIds])->field($fields)->select()->toArray();
            
            unset($userIds);
            unset($fields);
            
            foreach ($data as &$item) {
                foreach ($userList as $user) {
                    if($item['user_id'] == $user['user_id']){
                        $item['user'] = $user;
                        break;
                    }
                }
            }
            
        }
    }
    
    public function getUserCommonInfo($userId) {
        $user = $this->where(['user_id' => $userId])->field('register_time, nickname, avatar, sex, user_type, merchant_annual_expire, remark, mobile, wechat, label')->find()->toArray();
        if($user) {
            $user['user_id'] = $userId;
        }
        return $user;
    }
    
    public function getUserCommonInfoBySort(array $userIds) {
        $rows = $this->where('user_id', 'in', $userIds)->field('nickname, avatar, sex, user_id')->select()->toArray();
        $list = [];
        foreach ($userIds as $userId) {
            foreach ($rows as $k => $row) {
                if($userId == $row['user_id']){
                    array_push($list, $row);
                    unset($rows[$k]);
                    break;
                }
            }
        }
        
        return $list;
    }
}